/*
This file is part of buco.  
Copyright (c) 2012 Christopher Pawelzik <christopher@cpawelzik.com>

Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php 
*/ 
package buco.jsonrpc;

import java.io.IOException;
import java.io.Writer;

import org.json.simple.*;

import buco.core.api.*;

public class BundleCompositionConverter implements JSONStreamAware {

	BundleComposition composition; 
	
	public BundleCompositionConverter(BundleComposition composition) {
		this.composition = composition; 
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public void writeJSONString(Writer wr) throws IOException {
		JSONObject cmp = new JSONObject(); 
		JSONArray bundles = new JSONArray(); 
		JSONArray wires = new JSONArray();
		
		// Add all bundles
		for(Bundle bnd : composition.getBundles()) {
			bundles.add(new BundleConverter(bnd)); 
		}
		
		// Add wires... 		
		for(BundleWire wire : composition.getWires()) {
			wires.add(new BundleWireConverter(wire));
		}
				
		cmp.put("bundles", bundles);
		cmp.put("wires", wires);
		cmp.writeJSONString(wr);  
		
	}

}
